package com.ruoyi.analyse.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.analyse.domain.ChatMessage;
import com.ruoyi.analyse.service.IChatMessageService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;

/**
 * AI问答系统分析处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/analyse")
public class AnalyseController extends BaseController
{
    @Autowired
    private IChatMessageService chatMessageService;

    /**
     * 处理新问题
     */
    @PostMapping("/processQuestion")
    @PreAuthorize("@ss.hasPermi('deepSeek:dataset:add')")
    public AjaxResult processQuestion(@RequestBody ChatMessage chatMessage)
    {
        try {
            Map<String, Object> result = chatMessageService.processAndAnalyzeQuestion(chatMessage);
            return AjaxResult.success(result);
        } catch (Exception e) {
            logger.error("处理问题失败", e);
            return AjaxResult.error("处理问题失败: " + e.getMessage());
        }
    }

    /**
     * 处理历史问题数据
     */
    @PostMapping("/processHistoryData")
    @PreAuthorize("@ss.hasPermi('deepSeek:dataset:add')")
    public AjaxResult processHistoryData()
    {
        try {
            int count = chatMessageService.processHistoryData();
            return AjaxResult.success("成功处理 " + count + " 条历史数据");
        } catch (Exception e) {
            logger.error("处理历史数据失败", e);
            return AjaxResult.error("处理历史数据失败: " + e.getMessage());
        }
    }
}
